import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 计算正确率百分比
 */

public class percentage {
    public void calc(String keypath, String answerpath, String keyclass, int size) throws IOException {

        ImportExecl poi = new ImportExecl();
        File file = new File(answerpath);
        File flist[] = file.listFiles();
        int id = 1;
        List<List<String>> list = poi.read(keypath);  //答案路径
        List<String> right_ans = new ArrayList<String>();
        if (list != null) {
            for (int i = 4; i < list.size(); i++) {
                List<String> cellList = list.get(i);
                for (int j = 1; j < 2; j++) {
                    right_ans.add(cellList.get(1));  //将答案放在List中
                }
            }
        }
        String excelPath = "Report.xls";
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("report");
        Row row0 = sheet.createRow(0); //创建第一行
        Cell cell_1 = row0.createCell(0, Cell.CELL_TYPE_STRING);  //创建第一个单元格
        cell_1.setCellValue("题号");  //为第一个单元格赋值
        Cell cell_2 = row0.createCell(1, Cell.CELL_TYPE_STRING);
        cell_2.setCellValue("正确率");
        for (int i = 5; i <size; i++) {
            int count = 0;
            for ( File f : flist) {        //遍历文件夹下的文件
                List<List<String>> aai = poi.read(f.getAbsolutePath());
                if (list != null) {
                    //获取学生答案
                    if (aai.get(i).get(1).equals(right_ans.get(i-4))) {    //如果和标准答案匹配，count++
                        count++;
                    }
                }
            }
            Row row = sheet.createRow(id);
            Cell cell1 = row.createCell(0, Cell.CELL_TYPE_STRING);
            Cell cell2 = row.createCell(1, Cell.CELL_TYPE_STRING);
            cell1.setCellValue(id);
            cell2.setCellValue(100*((double)count / flist.length)+"%");
            id++;
            FileOutputStream outputStream = new FileOutputStream(excelPath);
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();

        }
    }
}
